import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';


import '../assets/color_config.dart';
import '../utils/log_utils.dart';
import '../utils/utils.dart';


class WebViewPage extends StatefulWidget {
  String? url = "";

  WebViewPage({Key? key, this.url}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _WebViewPageState();
  }
}

class _WebViewPageState extends State<WebViewPage> {
  String url = "";
  double _downLoadPercentage = 0;
  bool isShowLoad = true;
  late InAppWebViewController _webViewController;

  @override
  initState() {
    super.initState();
    url = widget.url ?? "";
    url = Utils.urlEncode(url);

  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: const Text("Add Payment"),
        ),
        body: url.isNotEmpty
            ? Stack(
                children: [
                  SafeArea(child: getWebView()),
                  isShowLoad
                      ? LinearProgressIndicator(
                          // 进度百分比
                          value: _downLoadPercentage,
                          // 进度条底色
                          backgroundColor: Colors.white,
                          // 已完成部分颜色
                          valueColor: AlwaysStoppedAnimation<Color>((ColorConfig.colorBlue),),
                        )
                      : const SizedBox(),
                ],
              )
            : const Center());
  }

  Widget getWebView() {
    return InAppWebView(
      initialUrlRequest: URLRequest(url: Uri.parse(url)),
      //initialFile: "assets/images/result.html",
      onWebViewCreated: (InAppWebViewController controller) {
        _webViewController = controller;
        _webViewController.addJavaScriptHandler(
          handlerName: "jsName",
          callback: (arguments) {
          },
        );
      },
      onLoadStart: (InAppWebViewController controller, Uri? url) {
        if (kDebugMode) {
          logger("onLoadStart===${url?.host}${url?.path}");
          logger(url?.queryParameters);
        }
      },
      onLoadStop: (InAppWebViewController controller, Uri? url) {
        if (kDebugMode) {
          logger("onLoadStop===${url?.host}${url?.path}");
          logger(url?.queryParameters);
        }
        isShowLoad = false;
        setState(() {});
      },
      onProgressChanged: (InAppWebViewController controller, int progress) {
        _downLoadPercentage = progress / 100;
        if (_downLoadPercentage == 1) {
          isShowLoad = false;
        }
        setState(() {});
      },
      onConsoleMessage: (InAppWebViewController controller, ConsoleMessage consoleMessage) {
        logger("""
              console output:
                message: ${consoleMessage.message}
                messageLevel: ${consoleMessage.messageLevel}
              """);
      },
    );
  }

  @override
  void dispose() {
    super.dispose();
    //移除注册代码
    _webViewController.removeAllUserScripts();
  }

}
